import { Component, Injector, ViewEncapsulation, ViewChild, ElementRef } from '@angular/core';
import { Router } from '@angular/router';
import { ModelServiceProxy, ModelListDto, LaunchListDto } from '@shared/service-proxies/service-proxies';
import { AppComponentBase } from '@shared/common/app-component-base';
import { appModuleAnimation } from '@shared/animations/routerTransition';
import { Paginator } from 'primeng/components/paginator/paginator';
import { LazyLoadEvent } from 'primeng/components/common/lazyloadevent';
import { Table } from 'primeng/table';

@Component({
    templateUrl: './models.component.html',
    styleUrls: ['./models.component.less'],
    encapsulation: ViewEncapsulation.None,
    animations: [appModuleAnimation()]
})
export class ModelsComponent extends AppComponentBase {
    @ViewChild('dataTable') dataTable: Table;
    @ViewChild('paginator') paginator: Paginator;
    filterText = '';
    batchs: LaunchListDto[] = new Array<LaunchListDto>();
    selectedModel: ModelListDto = new ModelListDto();
    selectedDate = new Date();
    constructor(injector: Injector, private _modelService: ModelServiceProxy, private _router: Router) {
        super(injector);
    }

    getModels(event?: LazyLoadEvent): void {
        this.primengTableHelper.showLoadingIndicator();
        this._modelService
            .getModels(
                this.filterText,
                this.primengTableHelper.getSorting(this.dataTable),
                this.primengTableHelper.getMaxResultCount(this.paginator, event),
                this.primengTableHelper.getSkipCount(this.paginator, event)
            )
            .subscribe(result => {
                this.primengTableHelper.records = result.items;
                this.primengTableHelper.totalRecordsCount = result.totalCount;
                this.primengTableHelper.hideLoadingIndicator();
                this.selectedModel = new ModelListDto();
                this.batchs = new Array<LaunchListDto>();
            });
    }

    deleteModel(model: ModelListDto): void {
        this.message.confirm('确定要删除型号' + model.code, isConfirmed => {
            if (isConfirmed) {
                this._modelService.deleteModel(model.id).subscribe(() => {
                    this.getModels();
                    this.notify.success(this.l('SuccessfullyDeleted'));
                });
            }
        });
    }

    selectModel(event): void {
        this.getBacths();
    }

    getBacths(): void {
        this._modelService.getLaunches(this.selectedModel.id, undefined, 1000, 0).subscribe(result => {
            this.batchs = result.items;
            console.log(result);
        });
    }

    deleteBacth(bacth): void {
        this.message.confirm('确定要删除批次' + bacth.code, isConfirmed => {
            if (isConfirmed) {
                this._modelService.deleteLaunch(bacth.id).subscribe(() => {
                    this.getBacths();
                    this.notify.success(this.l('SuccessfullyDeleted'));
                });
            }
        });
    }
}
